package com.zhiwei.httpclient;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class TiebaHttpClientDemo {
    
    public static Document executeHttpRequestGet(String url,String cookie) throws IOException {
        String result = null;
        Map<String, String> headerMap = new HashMap<String, String>();
        headerMap.put("User-Agent",
                "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36");
        headerMap.put("Accept",
                "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        headerMap.put("Accept-Encoding", "gzip, deflate");
        headerMap.put("Accept-Language", "zh-CN,zh;q=0.8");
        headerMap.put("Connection", "keep-alive");
        headerMap.put("host", "tieba.baidu.com");
        headerMap.put("cookie", cookie);
        Document document = null;
        CloseableHttpClient httpClient = null;
        try {
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom()
                    .setSocketTimeout(8000).setConnectTimeout(8000).build();
            httpClient = HttpClients.custom()
                    .setDefaultRequestConfig(requestConfig).build();
            if (headerMap != null) {
                for (Entry<String, String> header : headerMap.entrySet()) {
                    httpGet.setHeader(header.getKey(), header.getValue());
                }
            }
            result = EntityUtils
                    .toString(httpClient.execute(httpGet).getEntity());
            document = Jsoup.parse(result);
            return document;
        }catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (httpClient != null) {
                httpClient.close();
            }
        }
        return document;
    }
    
    public static void main(String[] args) {
        String cookie = "BAIDUID=81FD76825BFB1DBC6351266DB123EB21:FG=1; BIDUPSID=81FD76825BFB1DBC6351266DB123EB21; PSTM=1501037131; TIEBA_USERTYPE=dd08873efeab956aa1e0c945; TIEBAUID=54e32c68db371957ea22cdc3; bdshare_firstime=1501125737644; __cfduid=d16b52b83d2f221ab477675e9e648ecf91508555167; FP_UID=d736825bbdc65f4da0670b2ef76c5ebc; locale=zh; fixed_bar=1; FP_LASTTIME=1509323854226; PSINO=5; H_PS_PSSID=1446_13551_21094_18559_17001_20927; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; wise_device=0";
        
        try {
            String word = URLEncoder.encode("凯撒旅游","UTF-8");
            String url = "http://tieba.baidu.com/f/search/res?qw="+word+"&pn=1";
            Document document = TiebaHttpClientDemo.executeHttpRequestGet(url, cookie);
            System.out.println(document);
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}
